package cao.oschina.bean;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * 动弹
 * <p/>
 * Created by Haidy on 15/4/4.
 */
public class Tweet {

    public String id;
    public String portrait;
    public String author;
    public String authorid;
    public String body;
    public String attach;
    public String appclient;
    public String commentCount;
    public String pubDate;
    public String imgSmall;
    public String imgBig;
    public String likeCount;
    public String isLike;
    public List<Like> likeList;

    public static List<Tweet> parser(String xml) {
        List<Tweet> tweetList = new ArrayList<>();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
            Element root = document.getDocumentElement();
            NodeList newsList = root.getElementsByTagName("tweet");
            for (int i = 0; i < newsList.getLength(); i++) {
                Tweet tweet = new Tweet();
                NodeList childNodes = newsList.item(i).getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                        Node childNode = childNodes.item(j);
                        switch (childNode.getNodeName()) {
                            case "id":
                                tweet.id = childNode.getFirstChild().getNodeValue();
                                break;
                            case "body":
                                tweet.body = childNode.getFirstChild().getNodeValue();
                                break;
                            case "commentCount":
                                tweet.commentCount = childNode.getFirstChild().getNodeValue();
                                break;
                            case "author":
                                tweet.author = childNode.getFirstChild().getNodeValue();
                                break;
                            case "authorid":
                                tweet.authorid = childNode.getFirstChild().getNodeValue();
                                break;
                            case "pubDate":
                                tweet.pubDate = childNode.getFirstChild().getNodeValue();
                                break;
                            case "attach":
                                if (childNode.getFirstChild() != null) {
                                    tweet.attach = childNode.getFirstChild().getNodeValue();
                                }
                                break;
                            case "portrait":
                                if (childNode.getFirstChild() != null) {
                                    tweet.portrait = childNode.getFirstChild().getNodeValue();
                                }
                                break;
                            case "appclient":
                                tweet.appclient = childNode.getFirstChild().getNodeValue();
                                break;
                            case "imgSmall":
                                if (childNode.getFirstChild() != null) {
                                    tweet.imgSmall = childNode.getFirstChild().getNodeValue();
                                }
                                break;
                            case "imgBig":
                                if (childNode.getFirstChild() != null) {
                                    tweet.imgBig = childNode.getFirstChild().getNodeValue();
                                }
                                break;
                            case "likeCount":
                                tweet.likeCount = childNode.getFirstChild().getNodeValue();
                                break;
                            case "isLike":
                                tweet.isLike = childNode.getFirstChild().getNodeValue();
                                break;
                            case "likeList":
                                tweet.likeList = Like.parser(childNode.getChildNodes());
                                break;
                        }
                    }
                }
                tweetList.add(tweet);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return tweetList;
    }
}
